const querystring = {
	parse(str) {
		// console.log(str);// userName=zhangsan&age=100
		// console.log(str.split("&"));// [ 'userName=zhangsan', 'age=100' ]
		let obj = {};
		str.split("&").forEach((item) => {
			const [key, value] = item.split("=");
			obj[key] = value;
		});
		obj.__proto__ = null;
		return obj;
		
	},
	stringify(obj){
		// a=1&b=2&c=3
		// console.log(obj);// { a: 1, b: 2, c: 3 }
		// console.log(Object.keys(obj));// ["a","b","c"]
		// console.log(Object.keys(obj).map(key=>key+"="+obj[key]));// ["a=1","b=2","c=3"]
		// console.log(Object.keys(obj).map(key=>key+"="+obj[key]).join("&"));// a=1&b=2&c=3
		return Object.keys(obj).map(key=>key+"="+obj[key]).join("&");
	}
}


// 将查询字符串转为对象
// const query = "userName=zhangsan&age=100";
// [Object: null prototype] { userName: 'zhangsan', age: '100' }
// console.log(querystring.parse(query));

// 将对象转为查询字符串
const obj = {
	a:1,
	b:2,
	c:3
};
// a=1&b=2&c=3
console.log(querystring.stringify(obj));// a=1&b=2&c=3

